package com.ews.onlineexchange.service;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.QualificationCert;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.Trade;
import com.ews.onlineexchange.model.User;

@Model
public class LoginedUserUnauditCertList extends EntityQuery<QualificationCert> {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;

	@Inject
	UserService					userService;

	@Override
	protected List<Predicate> getRestrictions( CriteriaBuilder cb, Root<QualificationCert> entity ) {
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		User loginedUser = userService.getLoginedUser();
		Join<Trade, ServiceSupplier> supplier = entity.join( "serviceSupplier" );
		restrictions.add( cb.equal( supplier.get( "username" ), loginedUser.getUsername() ) );
		restrictions.add( cb.isFalse( entity.<Boolean>get( "auditResult" ) ) );
		restrictions.add( cb.isFalse( entity.<Boolean>get( "valid" ) ) );

		// String projectName = (String)getParameterValues().get("projectName");
		// if (!StringUtils.isBlank(projectName))
		// restrictions.add(cb.like(bulletin.get("projectName"),
		// getStringRestriction(projectName)));
		//
		// String serialNumber = (String)getParameterValues().get("serialNumber");
		// if (!StringUtils.isBlank(serialNumber))
		// restrictions.add(cb.like(entity.get("serialNumber"),
		// getStringRestriction(serialNumber)));

		return restrictions;
	}

	public LoginedUserUnauditCertList() {
		setMaxResults( MAX_RESULTS_NORMAL );
		setOrder( "auditTime desc,createDate desc" );
	}

	// @Override
	// protected void fetch(Root<QualificationCert> entity) {
	// entity.fetch("qualificationTemplates");
	// }

}
